package rbac.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.pandawork.core.entity.AbstractEntity;

/**
 * RBAC 用户 实体类.<br>
 * 记录了一个用户所属于的用户组.
 * 
 * @author Huadi
 * @author Zoufengyang
 */
@Entity
@Table(name = "t_rbac_user")
@NamedQueries( {
		@NamedQuery(name = "rbacUser.getAllRbacUsers", query = "from RbacUser"),
		@NamedQuery(name = "rbacUser.getRbacUserByName", query = "from RbacUser where name = :name"),
		@NamedQuery(name = "rbacUser.deleteRbacUserById", query = "delete from RbacUser where id = :id") })
public class RbacUser extends AbstractEntity {

	private static final long serialVersionUID = 2808470315352237066L;

	// ---=== Fields ===--- //
	/** <code>RbacUser</code> ID. */
	@Id
	@Column(name = "id", nullable = false, length = 11)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	/** <code>RbacUser</code> 名称. */
	@Column(name = "name", nullable = false, length = 255, unique = true)
	private String userName;

	/** <code>RbacUser</code> 所属用户组. */
	@ManyToMany(targetEntity = RbacUserGroup.class)
	@JoinTable(name = "t_rbac_user_group", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "group_id"))
	private Set<RbacUserGroup> userGroups;

	// ---=== Methods ===--- //
	/** 获得当前用户所拥有的 <code>RbacPermission</code>. */
	public List<RbacPermission> extractPermission() {
		List<RbacPermission> tempList = new ArrayList<RbacPermission>();
		for (RbacUserGroup ug : userGroups) {
			for (RbacRole r : ug.getRoles()) {
				for (RbacPermission p : r.getPermissions()) {
					tempList.add(p);
				}
			}
		}
		return tempList;
	}

	/** 获得当前用户所对应的资源字符串 <code>List</code>. */
	public List<String> extractActionStringToList() {
		List<String> actionStringList = new ArrayList<String>();
		for (RbacPermission permission : this.extractPermission()) {
			actionStringList.addAll(permission.extractResources());
		}
		return actionStringList;
	}

	/** 获得当前用户所对应的资源字符串 <code>Set</code>. */
	public Set<String> extractActionStringToSet() {
		Set<String> actionStringSet = new HashSet<String>();
		for (RbacPermission permission : this.extractPermission()) {
			actionStringSet.addAll(permission.extractResources());
		}
		return actionStringSet;
	}

	// ---=== Getters and Setters ===--- //
	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the userGroups
	 */
	public Set<RbacUserGroup> getUserGroups() {
		return userGroups;
	}

	/**
	 * @param userGroups
	 *            the userGroups to set
	 */
	public void setUserGroups(Set<RbacUserGroup> userGroups) {
		this.userGroups = userGroups;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	// ---=== Override ===--- //
	@Override
	public String toString() {
		return "id: " + id + ", userName: " + userName;
	}
}
